﻿using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using zhangmenren.IService;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using zhangmenren.Models.Enums;
using zhangmenren.Untils;

namespace zhangmenren.Controllers
{
	public class YuanChengJXController : Controller
	{

		private IBannerService _bannerService;

		private ICacheService _cacheService;

		private IWorksService worksService;
		private IChapterService chapterService;

		public YuanChengJXController(IBannerService bannerService, ICacheService cacheService, IWorksService _worksService, IChapterService _chapterService)
		{
			_bannerService = bannerService;
			_cacheService = cacheService;
			worksService = _worksService;
			chapterService = _chapterService;
		}

		/// <summary>
		/// 远程教学控制器
		/// </summary>
		/// <returns>The index.</returns>
		public async Task<IActionResult> Index(string key = "")
		{
			ViewBag.Banner = await GetData.GetBanner(_bannerService, _cacheService, "YuanChengJXBanner", BannerType.YCJX);
			var list = await worksService.Selelct(1, 50, key);
			//var list= chapterService

			return View(list.ToList().FindAll(p=>p.Type==WorkType.YCJX));
		}


		public async Task<IActionResult> Detail(int id)
		{
			var result = await  worksService.FindById(id);
			return View(result);
		}

        [HttpPost]
        public async Task<JsonResult> Search(string key)
        {
            var result = await worksService.Selelct(1, 50, key);
            var list = result.ToList().FindAll(p => p.Type == WorkType.YCJX);
            return Json(new { data = list, state = "ok", msg = "查询成功" });
        }

        /// <summary>
        /// ajax 根据关键字查询
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<JsonResult> GetWorksData(string key)
		{
			var list = await worksService.Selelct(1, 50, key);
			if (!list.Any())
			{
				return Json(new { state = "fail", msg = "无数据" });
			}
			return Json(new { state = "ok", msg = "查找成功", data = list });

		}


	}
}
